package com.example.wallpaperapp.Service;


import android.app.Service;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;

import com.example.wallpaperapp.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * 实现效果 -- 墙纸的切换 ， 背景图片
 * @author Administrator
 *
 */
public class WallService extends Service {

    public String[] img_list;
    int sourceId;
    public int[] res  = new int[]{R.drawable.image0,R.drawable.img_6,R.drawable.img_5};// 切换图片资源
    private WallpaperManager wallpaperManager; //墙纸管理器
    private int index; // 资源索引
    private int stop;



    // 绑定服务
    public IBinder onBind(Intent intent) {
        intent.getIntExtra("img_list",0);
        img_list=intent.getStringArrayExtra("img_list");

        return null;
    }

    /**
     * 启动服务
     * 每次启动开始获取资源
     */

    public void onStart(Intent intent, int startId) {

        super.onStart(intent, startId);


    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        img_list=intent.getStringArrayExtra("intent_img_list");
//        intent.getIntExtra("img_list",img_list[0]);
        stop = intent.getIntExtra("stop",0);
        stop = intent.getIntExtra("start",0);
        flags = START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

    // 创建服务
    public void onCreate() {

        super.onCreate();



        wallpaperManager = WallpaperManager.getInstance(WallService.this); // 获取壁纸管理器对象
        // 类型 ，执行延迟的时间，实行时间间隔，动作

        new Thread(){
          @Override
          public void run(){
              super.run();

              for (int i = 0; i < 7; i++) {
                  if(stop!=0){
                      break;
                  }
                  try{
                      if(i>=6){
                          i = 0;

                      }
                      try {
                          Thread.sleep(3000);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                      wallpaperManager.setBitmap(returnBitMap(img_list[i])); // 设置资源
                  }catch(Exception ex){
                      ex.printStackTrace();
                  }
              }
          }
        }.start();


    }

    /**
     * 将URL转化成bitmap形式
     *
     * @param url
     * @return bitmap type
     */
    private Bitmap bitmap;
    public Bitmap returnBitMap(final String url){

        new Thread(new Runnable() {
            @Override
            public void run() {
                URL imageurl = null;

                try {
                    imageurl = new URL(url);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                try {
                    if (imageurl!=null){
                        HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();
                        conn.setDoInput(true);
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        bitmap = BitmapFactory.decodeStream(is);
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        return bitmap;
    }

    // 销毁服务
    public void onDestroy() {
        super.onDestroy();
    }


}


